#include <iostream>
#include <set>
class ObserverBase  //观察者基类
{
public:
    ObserverBase(const std::string& name) : name(name) {}
    virtual void GetMsg(const std::string& words) = 0;  //获取消息

protected:
    std::string name;
};

class TargetBase  //被观察者基类
{
public:
    virtual void Update(const std::string& words) = 0;  //发布新消息
    virtual void AddObserver(ObserverBase *b)  //添加观察者
    {
        observers.insert(b);
    }

    void RemoveObserver(ObserverBase *b)  //移除观察者
    {
        observers.erase(b);
    }
protected:
    std::set<ObserverBase*> observers;  //观察者容器
};

class Target : public TargetBase //具体被观察者
{
public:
    void Update(const std::string& words)
    {
        for(auto ob : observers)
        {
            ob->GetMsg(words);
        }
    }
};

class Observer : public ObserverBase  //具体观察者
{
public:
    using ObserverBase::ObserverBase;
    void GetMsg(const std::string& words)
    {
        std::cout << name << "收到信息:" << words << std::endl;
    }
};

int main()
{
    ObserverBase *b = new Observer("张三");
    ObserverBase *b2 = new Observer("李四");

    TargetBase *t = new Target;
    t->AddObserver(b);
    t->AddObserver(b2);

    t->Update("今晚交作业");

    t->RemoveObserver(b);
    t->Update("没交作业的补作业");
    return 0;
}